package com.thb.pay.manage.comm.interceptor;

import com.thb.pay.api.sys.vo.resp.SysUserVo;
import com.thb.pay.core.cache.EhcacheUtils;
import com.thb.pay.core.enums.ExceptionEnum;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.interceptor.AbstractAuthenticationInterceptor;
import com.thb.pay.core.utils.Constant;
import com.thb.pay.core.utils.ContextHolder;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @类名称 AuthenticationInterceptor
 * @类描述 <pre></pre>
 * @作者 lucky
 * @创建时间 2019/10/11
 * @版本 v1.01
 */
@Component
public class AuthenticationInterceptor extends AbstractAuthenticationInterceptor {


    @Override
    public boolean authentication(HttpServletRequest request, HttpServletResponse response, Object handler) {
        SysUserVo sysUserVo = (SysUserVo) EhcacheUtils.getInstance().get(String.format(Constant.ACCESS_TOKEN, getToken(request)));
        if (sysUserVo == null) {
            throw RRException.build(ExceptionEnum.TOKEN_EXPIRE);
        }
        ContextHolder.setUserId(sysUserVo.getId());
        ContextHolder.setUserInfo(sysUserVo);
        return true;
    }
}
